// node 30_回文字符串.js

//如果一个字符串是回文字符串则返回true,否则返回false

let s1 = 'asdedsa'
let s2 = 'asdedsal'

//方法1
//遍历，头尾对应元素一一对比
function operateString1(params) {
    for(let i=0;i<(Math.floor(params.length /2));i++){
        if(params[i] != params[params.length-i-1])  return false
    }
    return true
}
console.log('s1:'+operateString1(s1))
console.log('s2:'+operateString1(s2))

//方法2
//把字符串转数组之后倒序再转字符串后进行比较
function operateString2(params) {
    if (params.split('').reverse().join('') != params) return false
    return true
}
console.log('s1:'+operateString2(s1))
console.log('s2:'+operateString2(s2))

//方法3
//递归实现
function operateString3(params) {
    if(params.length <= 1) return true
    if(params[0] != params[params.length-1]) return false
    return operateString3(params.substring(1,params.length-1))
}
console.log('s1:'+operateString3(s1))
console.log('s2:'+operateString3(s2))

//方法4
//递归实现2
function operateString4(params,index=0) {
    if(index > params.length / 2) return true
    if(params[index] != params[params.length-index-1]) return false
    return operateString4(params,index+1)
}
console.log('s1:'+operateString4(s1))
console.log('s2:'+operateString4(s2))